package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BusinessOperationDeleteDto;
import com.kaili.basic.pojo.dto.OutboundReturnAddDto;
import com.kaili.basic.pojo.dto.OutboundReturnQueryDto;
import com.kaili.basic.pojo.entity.OutboundReturn;
import com.kaili.basic.pojo.entity.OutboundReturnExport;
import com.kaili.basic.pojo.entity.OutboundReturnProduct;
import com.kaili.basic.service.OutboundReturnProductService;
import com.kaili.basic.service.OutboundReturnService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 出库退货 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "出库退货", description = "出库退货相关API")
public class OutboundReturnController {
    @Autowired
    OutboundReturnService outboundReturnService;
    @Autowired
    OutboundReturnProductService outboundProductService;

    @PostMapping(value = "/outboundReturns/addOutboundReturn")
    @ApiOperation(value = "新增出库退货",notes = "新增出库退货")
    public RestStatus addOutboundReturn(@RequestBody OutboundReturnAddDto outboundReturnAddDto){
        outboundReturnService.addOutboundReturn(outboundReturnAddDto);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/outboundReturns/deleteOutboundReturns")
    @ApiOperation(value = "根据主键id删除记录", notes = "根据主键id删除记录")
    public RestStatus deleteOutboundReturns(@RequestBody BusinessOperationDeleteDto deleteDto) {
        outboundReturnService.deleteOutboundReturns(deleteDto);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/outboundReturns/queryOutboundReturn")
    @ApiOperation(value = "条件查询出库退货信息",notes = "条件查询出库退货信息")
    public Page<OutboundReturn> queryOutboundReturn(@ModelAttribute OutboundReturnQueryDto outboundReturnQueryDto) {
        return outboundReturnService.queryOutboundReturn(new Page<OutboundReturn>(outboundReturnQueryDto.getPage(),outboundReturnQueryDto.getPageSize()),outboundReturnQueryDto);
    }

    @GetMapping(value = "/outboundReturns/{returnId}/queryOutboundReturnById")
    @ApiOperation(value = "根据出库退货主键id查询出库退货信息",notes = "根据出库退货主键id查询出库退货信息")
    public OutboundReturn queryOutboundReturnById(@PathVariable("returnId") Long returnId) {
        return outboundReturnService.queryOutboundReturnById(returnId);
    }

    @GetMapping(value = "/outboundReturns/{id}/queryOutboundReturnProductById")
    @ApiOperation(value = "根据出库退货产品主键id查询出库退货产品信息",notes = "根据出库退货产品主键id查询出库退货产品信息")
    public OutboundReturnProduct queryOutboundReturnProductById(@PathVariable("id") Long id) {
        return outboundProductService.getOutboundReturnProductById(id);
    }

    @PutMapping(value = "/outboundReturns/{returnId}/orderConfirmation")
    @ApiOperation(value = "订单确认",notes = "订单确认")
    public RestStatus orderConfirmation(@PathVariable("returnId") Long returnId) {
        OutboundReturn outboundReturn = new OutboundReturn();
        outboundReturn.setReturnId(returnId);
        outboundReturn.setOrderStatus(2);
        outboundReturn.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        outboundReturnService.updateAffirmOrCancel(outboundReturn);
        return CommonStatus.ORDER_CONFIRMATION;
    }

    @PutMapping(value = "/outboundReturns/{returnId}/orderCancellation")
    @ApiOperation(value = "订单取消",notes = "订单取消")
    public RestStatus orderCancellation(@PathVariable("returnId") Long returnId) {
        OutboundReturn outboundReturn = new OutboundReturn();
        outboundReturn.setReturnId(returnId);
        outboundReturn.setOrderStatus(3);
        outboundReturn.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        outboundReturnService.updateAffirmOrCancel(outboundReturn);
        return CommonStatus.ORDER_CANCEL;
    }

    @GetMapping("/outboundReturns/export")
    @ApiOperation(value = "导出出库退货信息")
    public void export(HttpServletResponse response, @ModelAttribute OutboundReturnQueryDto outboundReturnQueryDto) throws RestException {
        List<OutboundReturnExport> list = outboundReturnService.queryOutboundReturnExport(outboundReturnQueryDto);
        ExcelUtil.exportExcel(list,"出库退货信息","sheet1", OutboundReturnExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

